package jtacck.labmedicine;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * @deprecated
 * 
 * Overlay used to draw the noticeable blue circles over a location on the map
 * portion of the Android application.
 * 
 * @see com.google.android.maps.Overlay
 * 
 * @author Andrew
 * 
 */
public class CircleOverlay extends Overlay {

    /**
     * Current location of the mobile device.
     */
    Location currentLocation;

    /**
     * The image that is drawn representing the accuracy of the Network/GPS.
     */
    private Paint accuracyPaint;

    /**
     * The center of the circle aka where we're at.
     */
    private Point center;

    /**
     * The left "side" of the circle so that we can calculate the radius.
     */
    private Point left;

    // Temporarily not used in correspondance with the drawable.
    // private Drawable drawable;
    // private int width;
    // private int height;

    /**
     * Constructs a new circle overlay at the location.
     * 
     * @param location
     *            - the Location of where a circle is to be drawn
     */
    public CircleOverlay(Location location) {
        currentLocation = location;
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.MapView, boolean)
     * 
     * Overridden draw method that dras he actual circle.
     */
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);

        /** Set the antialiasing and size of stroke **/
        accuracyPaint = new Paint();
        accuracyPaint.setAntiAlias(true);
        accuracyPaint.setStrokeWidth(2.0f);

        // The drawable
        // drawable = mapView.getContext().getResources()
        // .getDrawable(R.drawable.maps_icon);
        // width = drawable.getIntrinsicWidth();
        // height = drawable.getIntrinsicHeight();
        
        /** Set up variables **/
        center = new Point();
        left = new Point();
        double latitude;
        double longitude;
        float accuracy;
        Projection projection = mapView.getProjection();

        /** Get attributes to the location **/
        latitude = currentLocation.getLatitude();
        longitude = currentLocation.getLongitude();
        accuracy = currentLocation.getAccuracy();

        float[] result = new float[1];

        Location.distanceBetween(latitude, longitude, latitude, longitude + 1,
                result);
        float longitudeLineDistance = result[0];

        GeoPoint leftGeo = new GeoPoint(
                (int) (latitude * 1E6),
                (int) ((longitude - accuracy / (longitudeLineDistance * 2.5)) * 1E6));
        projection.toPixels(leftGeo, left);
        projection.toPixels(new GeoPoint(
                (int) (currentLocation.getLatitude() * 1E6),
                (int) (currentLocation.getLongitude() * 1E6)), center);
        int radius = center.x - left.x;

        /** Set color and style of each circle and it's location **/
        accuracyPaint.setColor(0xff6666ff);
        accuracyPaint.setStyle(Style.STROKE);
        canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

        accuracyPaint.setColor(0x186666ff);
        accuracyPaint.setStyle(Style.FILL);
        canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

        // Add back after finding a suitable placemark for location.
        // Might decide to use MyLocationOverlay
        // drawable.setBounds(center.x - width / 2, center.y - height / 2,
        // center.x + width / 2, center.y + height / 2);
        // drawable.draw(canvas);
    }

}
